package com.epam.web_project.bank.command.customer_commands;

import java.io.IOException;
import java.util.Date;
import java.util.Locale;
import java.util.ResourceBundle;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.epam.web_project.bank.command.Command;
import com.epam.web_project.bank.dao.account_dao.AccountDAO;
import com.epam.web_project.bank.dao.factory.MySQLDAOFactory;
import com.epam.web_project.bank.db.ConnectionPool;
import com.epam.web_project.bank.entity.Account;
import com.epam.web_project.bank.util.AppPropertiesLoader;

public class CheckAccountBlockCommand implements Command {

	//go to transfer page if the account is not blocked
	//used by a regular customer
	@Override
	public String execute(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		AppPropertiesLoader propLoader = AppPropertiesLoader.getInstance();
		if (request.getParameter("accId").equals("")) {
			return propLoader.getProperty("show_main_command");
		}
		int senderAccountId = Integer.valueOf(request.getParameter("accId"));
		MySQLDAOFactory daoFactory = new MySQLDAOFactory();
		daoFactory.setConnectionPool(ConnectionPool.getInstance());
		AccountDAO accountDAO = daoFactory.getAccountDAO();
		Account senderAccount = accountDAO.findById(senderAccountId);
		HttpSession session = request.getSession();
		Locale locale=request.getLocale();
		ResourceBundle bundle=ResourceBundle.getBundle("i18n", locale);
		if (senderAccount.getCard().getExpires().after(new Date())) {
			if (!senderAccount.isBlocked()) {
				//request.setAttribute("senderAcc", senderAccount);
				session.setAttribute("senderAcc", senderAccount);
				return propLoader.getProperty("show_transfer_command");
			} else {
				session.setAttribute("message",
						bundle.getString("java.acc_blocked"));
				return propLoader.getProperty("show_main_command");
			}
		} else {
			session.setAttribute("message", bundle.getString("java.card_exp"));
			return propLoader.getProperty("show_main_command");
		}

	}

}
